/**
 * Copyright (C) 2009 aileron.cc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cc.aileron.text;

import static cc.aileron.workflow.WorkflowMethod.*;
import static cc.aileron.workflow.WorkflowTransition.*;
import cc.aileron.commons.resource.ResourceNotFoundException;
import cc.aileron.template.parser.ParserMethodNotFoundException;
import cc.aileron.template.reader.TemplateSyntaxEexception;
import cc.aileron.text.process.TextDisplayProcess;
import cc.aileron.text.process.TextRegistryProcess;
import cc.aileron.text.process.TextValidator;
import cc.aileron.text.resources.TextResource;
import cc.aileron.workflow.WorkflowBinder;
import cc.aileron.workflow.WorkflowConfigure;
import cc.aileron.workflow.WorkflowTransition;
import cc.aileron.wsgi.extension.template.TemplateProcessProviderFactory;

import com.google.inject.Inject;

/**
 * @author Aileron
 */
public class ApplicationConfigure implements WorkflowConfigure
{
    @Override
    public void configure(final WorkflowBinder binder)
    {
        /*
         * index
         */
        binder.bind(TextResource.class)
            .uri("/")
            .method(GET)
            .to()
            .process(LOCALREDIRECT, "/${message.uriKey}.html");

        /*
         * display
         */
        binder.bind(TextResource.class)
            .uri("/${message.uriKey}.html")
            .method(GET)
            .to()
            .process(TextValidator.class, html.edit())
            .process(TextDisplayProcess.class)
            .process(html.display());

        /*
         * edit
         */
        binder.bind(TextResource.class)
            .uri("/${message.uriKey}.html")
            .method(POST)
            .overrideKey("edit")
            .to()
            .process(TextValidator.class, WorkflowTransition.NOT_FOUND)
            .process(TextDisplayProcess.class)
            .process(html.edit());

        /*
         * post html-post
         */
        binder.bind(TextResource.class)
            .uri("/${message.uriKey}.html")
            .method(POST)
            .to()
            .requestParameterKeys("message")
            .process(TextRegistryProcess.class)
            .process(WorkflowTransition.LOCALREDIRECT,
                    "/${message.uriKey}.html");
    }

    /**
     * @param factory
     * @throws ResourceNotFoundException
     * @throws ParserMethodNotFoundException
     * @throws TemplateSyntaxEexception
     */
    @Inject
    public ApplicationConfigure(final TemplateProcessProviderFactory factory) throws TemplateSyntaxEexception,
            ParserMethodNotFoundException,
            ResourceNotFoundException
    {
        this.html = factory.get(ApplicationTemplateHtml.class);
    }

    private final cc.aileron.text.ApplicationTemplateHtml html;
}
